central keyboard work with polling method
- central bonding still needs some more work for reconnection.
This commit is contained in:
1 parent
1fd9cbedc5
commit
f16576b70c
4 files changed
+228
-49
No files matched your search
@@ -18,14 +18,23 @@
|
||||
*/
|
||||
#include <bluefruit.h>
|
||||
|
||||
// Polling or callback implementation
|
||||
#define POLLING 1
|
||||
|
||||
BLEClientHidAdafruit hid;
|
||||
|
||||
// Last checked report, to detect if we receive a new one with polling method
|
||||
hid_keyboard_report_t last_kbd_report = { 0 };
|
||||
hid_mouse_report_t last_mse_report = { 0 };
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
|
||||
Serial.println("--------------------------------------------------\n");
|
||||
|
||||
Bluefruit.configCentralConn(23, 3, 1, 3);
|
||||
|
||||
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
|
||||
// SRAM usage required by SoftDevice will increase dramatically with number of connections
|
||||
@@ -83,6 +92,13 @@ void connect_callback(uint16_t conn_handle)
|
||||
{
|
||||
Serial.println("Found it");
|
||||
|
||||
// HID device mostly require pairing/bonding
|
||||
if ( !Bluefruit.Gap.requestPairing(conn_handle) )
|
||||
{
|
||||
Serial.print("Failed to paired");
|
||||
return;
|
||||
}
|
||||
|
||||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.hid_information.xml
|
||||
uint8_t hidInfo[4];
|
||||
hid.getHidInfo(hidInfo);
|
||||
@@ -90,11 +106,17 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.printf("HID version: %d.%d\n", hidInfo[0], hidInfo[1]);
|
||||
Serial.print("Country code: "); Serial.println(hidInfo[2]);
|
||||
Serial.printf("HID Flags : 0x%02X\n", hidInfo[3]);
|
||||
|
||||
// BLEClientHidAdafruit currently only suports Boot Protocol Mode
|
||||
// for Keyboard and Mouse. Let's set the protocol mode on prph to Boot Mode
|
||||
hid.setProtocolMode(HID_PROTOCOL_MODE_BOOT);
|
||||
|
||||
// Enable Keyboard report notification if present on prph
|
||||
if ( hid.keyboardPresent() ) hid.enableKeyboard();
|
||||
|
||||
// Enable Mouse report notification if present on prph
|
||||
if ( hid.mousePresent() ) hid.enableMouse();
|
||||
|
||||
|
||||
// Serial.println("Enable TXD's notify");
|
||||
// clientUart.enableTXD();
|
||||
|
||||
Serial.println("Ready to receive from peripheral");
|
||||
}else
|
||||
{
|
||||
@@ -118,27 +140,72 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
Serial.println("Disconnected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when uart received data
|
||||
* @param uart_svc Reference object to the service where the data
|
||||
* arrived. In this example it is clientUart
|
||||
*/
|
||||
//void bleuart_rx_callback(BLEClientUart& uart_svc)
|
||||
//{
|
||||
// Serial.print("[RX]: ");
|
||||
//
|
||||
// while ( uart_svc.available() )
|
||||
// {
|
||||
// Serial.print( (char) uart_svc.read() );
|
||||
// }
|
||||
//
|
||||
// Serial.println();
|
||||
//}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// nothing to do
|
||||
|
||||
#if POLLING
|
||||
// nothing to do if hid not discovered
|
||||
if ( !hid.discovered() ) return;
|
||||
|
||||
/*------------- Polling Keyboard -------------*/
|
||||
hid_keyboard_report_t kbd_report;
|
||||
|
||||
// Get latest report
|
||||
hid.getKeyboardReport(&kbd_report);
|
||||
|
||||
// Check with last report to see if there is new arrival report
|
||||
if ( memcmp(&last_kbd_report, &kbd_report, sizeof(kbd_report)) )
|
||||
{
|
||||
bool shifted = false;
|
||||
|
||||
if ( kbd_report.modifier )
|
||||
{
|
||||
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL) )
|
||||
{
|
||||
Serial.print("Ctrl ");
|
||||
}
|
||||
|
||||
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) )
|
||||
{
|
||||
Serial.print("Shift ");
|
||||
|
||||
shifted = true;
|
||||
}
|
||||
|
||||
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTALT | KEYBOARD_MODIFIER_RIGHTALT) )
|
||||
{
|
||||
Serial.print("Alt ");
|
||||
}
|
||||
}
|
||||
|
||||
for(uint8_t i=0; i<6; i++)
|
||||
{
|
||||
uint8_t kc = kbd_report.keycode[i];
|
||||
char ch = 0;
|
||||
|
||||
if ( kc < 128 )
|
||||
{
|
||||
ch = shifted ? HID_KEYCODE_TO_ASCII[kc].shifted : HID_KEYCODE_TO_ASCII[kc].ascii;
|
||||
}else
|
||||
{
|
||||
// non-US keyboard !!??
|
||||
}
|
||||
|
||||
// Printable
|
||||
if (ch)
|
||||
{
|
||||
Serial.print(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update last report
|
||||
memcpy(&last_kbd_report, &kbd_report, sizeof(kbd_report));
|
||||
|
||||
|
||||
// polling interval is 5 ms
|
||||
delay(5);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -470,6 +470,35 @@ SECMODE_ENC_WITH_MITM LITERAL1
|
||||
SECMODE_SIGNED_NO_MITM LITERAL1
|
||||
SECMODE_SIGNED_WITH_MITM LITERAL1
|
||||
|
||||
#######################################
|
||||
# HID Constants
|
||||
#######################################
|
||||
|
||||
HID_PROTOCOL_MODE_BOOT LITERAL1
|
||||
HID_PROTOCOL_MODE_REPORT LITERAL1
|
||||
|
||||
MOUSE_BUTTON_LEFT LITERAL1
|
||||
MOUSE_BUTTON_RIGHT LITERAL1
|
||||
MOUSE_BUTTON_MIDDLE LITERAL1
|
||||
MOUSE_BUTTON_BACKWARD LITERAL1
|
||||
MOUSE_BUTTON_FORWARD LITERAL1
|
||||
|
||||
KEYBOARD_MODIFIER_LEFTCTRL LITERAL1
|
||||
KEYBOARD_MODIFIER_LEFTSHIFT LITERAL1
|
||||
KEYBOARD_MODIFIER_LEFTALT LITERAL1
|
||||
KEYBOARD_MODIFIER_LEFTGUI LITERAL1
|
||||
KEYBOARD_MODIFIER_RIGHTCTRL LITERAL1
|
||||
KEYBOARD_MODIFIER_RIGHTSHIFT LITERAL1
|
||||
KEYBOARD_MODIFIER_RIGHTALT LITERAL1
|
||||
KEYBOARD_MODIFIER_RIGHTGUI LITERAL1
|
||||
|
||||
KEYBOARD_LED_NUMLOCK LITERAL1
|
||||
KEYBOARD_LED_CAPSLOCK LITERAL1
|
||||
KEYBOARD_LED_SCROLLLOCK LITERAL1
|
||||
KEYBOARD_LED_COMPOSE LITERAL1
|
||||
KEYBOARD_LED_KANA LITERAL1
|
||||
|
||||
|
||||
#######################################
|
||||
# ANCS Constants
|
||||
#######################################
|
||||
|
||||
@@ -40,11 +40,29 @@ BLEClientHidAdafruit::BLEClientHidAdafruit(void)
|
||||
: BLEClientService(UUID16_SVC_HUMAN_INTERFACE_DEVICE),
|
||||
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
|
||||
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
|
||||
_mouse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
|
||||
_mse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
|
||||
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT)
|
||||
{
|
||||
_kbd_cb = NULL;
|
||||
_mse_cb = NULL;
|
||||
varclr(&_last_kbd_report);
|
||||
varclr(&_last_mse_report);
|
||||
}
|
||||
|
||||
|
||||
void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService();
|
||||
svc._handle_kbd_input(data, len);
|
||||
}
|
||||
|
||||
void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService();
|
||||
svc._handle_mse_input(data, len);
|
||||
}
|
||||
|
||||
|
||||
bool BLEClientHidAdafruit::begin(void)
|
||||
{
|
||||
// Invoke base class begin()
|
||||
@@ -57,12 +75,22 @@ bool BLEClientHidAdafruit::begin(void)
|
||||
_kbd_boot_input.begin(this);
|
||||
_kbd_boot_output.begin(this);
|
||||
|
||||
_mouse_boot_input.begin(this);
|
||||
_mse_boot_input.begin(this);
|
||||
|
||||
|
||||
// set notify callback
|
||||
// _kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
|
||||
// _mouse_boot_input.setNotifyCallback(mouse_client_notify_cb);
|
||||
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
|
||||
_mse_boot_input.setNotifyCallback(mse_client_notify_cb);
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::setKeyboardReportCallback(kbd_callback_t fp)
|
||||
{
|
||||
_kbd_cb = fp;
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::setMouseReportCallback(mse_callback_t fp)
|
||||
{
|
||||
_mse_cb = fp;
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
|
||||
@@ -72,7 +100,7 @@ bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
|
||||
|
||||
// Discover all characteristics
|
||||
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mouse_boot_input, _hid_info, _hid_control);
|
||||
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mse_boot_input, _hid_info, _hid_control);
|
||||
|
||||
VERIFY( _protcol_mode.discovered() && _hid_info.discovered() && _hid_control.discovered() );
|
||||
VERIFY ( keyboardPresent() || mousePresent() );
|
||||
@@ -81,6 +109,9 @@ bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Info
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::getHidInfo(uint8_t info[4])
|
||||
{
|
||||
return 4 == _hid_info.read(info, 4);
|
||||
@@ -94,32 +125,70 @@ uint8_t BLEClientHidAdafruit::getCountryCode(void)
|
||||
return info[2];
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::setProtocolMode(uint8_t mode)
|
||||
{
|
||||
return _protcol_mode.write8(mode);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Keyboard
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::keyboardPresent(void)
|
||||
{
|
||||
return _kbd_boot_input.discovered() && _kbd_boot_output.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::mousePresent(void)
|
||||
{
|
||||
return _mouse_boot_input.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableKeyboard(void)
|
||||
{
|
||||
_kbd_boot_input.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableMouse(void)
|
||||
{
|
||||
_mouse_boot_input.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::disableKeyboard(void)
|
||||
{
|
||||
_kbd_boot_input.disableNotify();
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::_handle_kbd_input(uint8_t* data, uint16_t len)
|
||||
{
|
||||
varclr(&_last_kbd_report);
|
||||
memcpy(&_last_kbd_report, data, len);
|
||||
|
||||
if ( _kbd_cb ) _kbd_cb(&_last_kbd_report);
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::getKeyboardReport(hid_keyboard_report_t* report)
|
||||
{
|
||||
memcpy(report, &_last_kbd_report, sizeof(hid_keyboard_report_t));
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Mouse
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::mousePresent(void)
|
||||
{
|
||||
return _mse_boot_input.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableMouse(void)
|
||||
{
|
||||
_mse_boot_input.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::disableMouse(void)
|
||||
{
|
||||
_mouse_boot_input.disableNotify();
|
||||
_mse_boot_input.disableNotify();
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::_handle_mse_input(uint8_t* data, uint16_t len)
|
||||
{
|
||||
varclr(&_last_mse_report);
|
||||
memcpy(&_last_mse_report, data, len);
|
||||
|
||||
if ( _mse_cb ) _mse_cb(&_last_mse_report);
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::getMouseReport(hid_mouse_report_t* report)
|
||||
{
|
||||
memcpy(report, &_last_mse_report, sizeof(hid_mouse_report_t));
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
{
|
||||
public:
|
||||
// Callback Signatures
|
||||
typedef void (*kbd_callback_t ) (uint8_t modifier, uint8_t keycode[6]);
|
||||
typedef void (*mouse_callback_t ) (uint8_t buttons, int8_t x, int8_t y, int8_t wheel, int8_t pan);
|
||||
typedef void (*kbd_callback_t ) (hid_keyboard_report_t* report);
|
||||
typedef void (*mse_callback_t ) (hid_mouse_report_t* report);
|
||||
|
||||
BLEClientHidAdafruit(void);
|
||||
|
||||
@@ -58,24 +58,32 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
bool getHidInfo(uint8_t info[4]);
|
||||
uint8_t getCountryCode(void);
|
||||
|
||||
// Check if peripheral support keyboard/mouse
|
||||
bool keyboardPresent(void);
|
||||
bool mousePresent(void);
|
||||
bool setProtocolMode(uint8_t mode);
|
||||
|
||||
// Enable/Disable report notification for keyboard/mouse
|
||||
// Keyboard API
|
||||
bool keyboardPresent(void);
|
||||
bool enableKeyboard(void);
|
||||
bool disableKeyboard(void);
|
||||
|
||||
void getKeyboardReport(hid_keyboard_report_t* report);
|
||||
|
||||
// Mouse API
|
||||
bool mousePresent(void);
|
||||
bool enableMouse(void);
|
||||
bool disableMouse(void);
|
||||
|
||||
void getMouseReport(hid_mouse_report_t* report);
|
||||
|
||||
// Report callback
|
||||
void setKeyboardReportCallback(kbd_callback_t fp);
|
||||
void setMouseReportCallback(mouse_callback_t fp);
|
||||
void setMouseReportCallback(mse_callback_t fp);
|
||||
|
||||
private:
|
||||
kbd_callback_t _kbd_cb;
|
||||
mouse_callback_t _mouse_cb;
|
||||
protected:
|
||||
kbd_callback_t _kbd_cb;
|
||||
mse_callback_t _mse_cb;
|
||||
|
||||
hid_keyboard_report_t _last_kbd_report;
|
||||
hid_mouse_report_t _last_mse_report;
|
||||
|
||||
// Only support Boot protocol for keyboard and Mouse
|
||||
BLEClientCharacteristic _protcol_mode;
|
||||
@@ -85,7 +93,13 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
BLEClientCharacteristic _kbd_boot_input;
|
||||
BLEClientCharacteristic _kbd_boot_output;
|
||||
|
||||
BLEClientCharacteristic _mouse_boot_input;
|
||||
BLEClientCharacteristic _mse_boot_input;
|
||||
|
||||
void _handle_kbd_input(uint8_t* data, uint16_t len);
|
||||
void _handle_mse_input(uint8_t* data, uint16_t len);
|
||||
|
||||
friend void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
|
||||
friend void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user