Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we please merge latest from ESP32-BLE-Keyboard #12

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b64348d
Fixes to media keys connect and disconnect.
mwinters-stuff Feb 23, 2020
e43fdd6
Merge pull request #16 from mwinters-stuff/master
T-vK Feb 27, 2020
7e17d03
doc: Fix formatting for example in README
T-vK Feb 27, 2020
2dd80af
Bump version for new release
T-vK Feb 27, 2020
29195fd
Expose server started hook
dvv Apr 22, 2020
12b4214
Update BleKeyboard.cpp
dvv Apr 22, 2020
07e4bda
Merge pull request #19 from dvv/patch-1
T-vK Apr 26, 2020
f7d6f3f
Set ScanResponse to off so the advertising will actually advertise th…
aovestdipaperino Oct 19, 2020
92a056a
Merge pull request #49 from aovestdipaperino/master
T-vK Oct 20, 2020
cd8ed26
Fix typo in README #50
T-vK Oct 20, 2020
959581c
Update feature checklist in README
T-vK Oct 20, 2020
928c093
Bump version for next release
T-vK Oct 20, 2020
361806a
Fix reconnection issue #84 (#86)
aovestdipaperino Apr 21, 2021
9b2444b
added setName function to allow changing the name of the device using…
millst Jul 19, 2021
6cbd6e3
added setName to BleKeyboard.h (#100)
millst Jul 19, 2021
2c13a70
Implemented NimBLE (optional)
sivar2311 Aug 27, 2021
23c95a6
Added section about how to use NimBLE-Mode
sivar2311 Aug 27, 2021
ce2cd53
fixed: include NimBLEHIDDevice in NIMBLE mode
sivar2311 Aug 27, 2021
65b6563
Limit device Name and Manufacturer to 15 chars
sivar2311 Aug 27, 2021
8b40e0c
changed default deviceName to "ESP32 Keyboard"
sivar2311 Aug 27, 2021
1dfbed9
implemented setDelay for NimBLE mode
sivar2311 Aug 28, 2021
f4e0112
Merge pull request #111 from sivar2311/master
T-vK Sep 10, 2021
5c63369
Updated the Readme
T-vK Sep 10, 2021
32c5884
Bump version for new release
T-vK Sep 10, 2021
b9d0955
Fixed settings for platformio
sivar2311 Sep 16, 2021
01f5dd9
Fix for #115
sivar2311 Sep 17, 2021
d4b5d9d
Added functions to set VendorID, ProductID, Version
sivar2311 Sep 28, 2021
7984db0
Merge pull request #117 from sivar2311/master
T-vK Oct 1, 2021
4d93a04
Bump version for next release
T-vK Oct 1, 2021
364ab94
Numpad Key definitions, PRTSC and version info
DustinWatts Nov 9, 2021
8e184d5
Update BleKeyboard.h
DustinWatts Nov 9, 2021
82959f2
Merge pull request #126 from DustinWatts/patch-1
T-vK Nov 9, 2021
0c52ec7
Update to reflect code.
ScaredyCat Jan 6, 2022
5cffe1d
Remove text
ScaredyCat Jan 6, 2022
6cf1902
Comment out CTRL+ALT+DEL
ScaredyCat Jan 6, 2022
15ff2f1
Merge pull request #134 from ScaredyCat/patch-2
T-vK Jan 6, 2022
183afac
Merge pull request #135 from ScaredyCat/patch-3
T-vK Jan 6, 2022
c96bc0e
Fixed USE_NIMBLE
sivar2311 Feb 6, 2022
e54ec21
Fixed typo
sivar2311 Feb 6, 2022
f8dd485
Bump version for next release
T-vK Feb 14, 2022
0b14086
Fixed MediaKeys releasing on ReleaseAll
muddyfeet Mar 24, 2024
b7aaf9b
Merge pull request #290 from muddyfeet/master
T-vK Mar 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions BleConnectionStatus.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions BleConnectionStatus.h

This file was deleted.

178 changes: 133 additions & 45 deletions BleKeyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "BleKeyboard.h"

#if defined(USE_NIMBLE)
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
#include <NimBLEUtils.h>
#include <NimBLEHIDDevice.h>
#else
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "BLE2902.h"
#include "BLEHIDDevice.h"
#endif // USE_NIMBLE
#include "HIDTypes.h"
#include <driver/adc.h>
#include "sdkconfig.h"

#include "BleConnectionStatus.h"
#include "KeyboardOutputCallbacks.h"
#include "BleKeyboard.h"

#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
Expand Down Expand Up @@ -89,25 +95,63 @@ static const uint8_t _hidReportDescriptor[] = {
END_COLLECTION(0) // END_COLLECTION
};

BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : hid(0)
{
this->deviceName = deviceName;
this->deviceManufacturer = deviceManufacturer;
this->batteryLevel = batteryLevel;
this->connectionStatus = new BleConnectionStatus();
}
BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel)
: hid(0)
, deviceName(std::string(deviceName).substr(0, 15))
, deviceManufacturer(std::string(deviceManufacturer).substr(0,15))
, batteryLevel(batteryLevel) {}

void BleKeyboard::begin(void)
{
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
BLEDevice::init(deviceName);
BLEServer* pServer = BLEDevice::createServer();
pServer->setCallbacks(this);

hid = new BLEHIDDevice(pServer);
inputKeyboard = hid->inputReport(KEYBOARD_ID); // <-- input REPORTID from report map
outputKeyboard = hid->outputReport(KEYBOARD_ID);
inputMediaKeys = hid->inputReport(MEDIA_KEYS_ID);

outputKeyboard->setCallbacks(this);

hid->manufacturer()->setValue(deviceManufacturer);

hid->pnp(0x02, vid, pid, version);
hid->hidInfo(0x00, 0x01);


#if defined(USE_NIMBLE)

BLEDevice::setSecurityAuth(true, true, true);

#else

BLESecurity* pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);

#endif // USE_NIMBLE

hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
hid->startServices();

onStarted(pServer);

advertising = pServer->getAdvertising();
advertising->setAppearance(HID_KEYBOARD);
advertising->addServiceUUID(hid->hidService()->getUUID());
advertising->setScanResponse(false);
advertising->start();
hid->setBatteryLevel(batteryLevel);

ESP_LOGD(LOG_TAG, "Advertising started!");
}

void BleKeyboard::end(void)
{
}

bool BleKeyboard::isConnected(void) {
return this->connectionStatus->connected;
return this->connected;
}

void BleKeyboard::setBatteryLevel(uint8_t level) {
Expand All @@ -116,41 +160,30 @@ void BleKeyboard::setBatteryLevel(uint8_t level) {
this->hid->setBatteryLevel(this->batteryLevel);
}

void BleKeyboard::taskServer(void* pvParameter) {
BleKeyboard* bleKeyboardInstance = (BleKeyboard *) pvParameter; //static_cast<BleKeyboard *>(pvParameter);
BLEDevice::init(bleKeyboardInstance->deviceName);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(bleKeyboardInstance->connectionStatus);

bleKeyboardInstance->hid = new BLEHIDDevice(pServer);
bleKeyboardInstance->inputKeyboard = bleKeyboardInstance->hid->inputReport(KEYBOARD_ID); // <-- input REPORTID from report map
bleKeyboardInstance->outputKeyboard = bleKeyboardInstance->hid->outputReport(KEYBOARD_ID);
bleKeyboardInstance->inputMediaKeys = bleKeyboardInstance->hid->inputReport(MEDIA_KEYS_ID);
bleKeyboardInstance->connectionStatus->inputKeyboard = bleKeyboardInstance->inputKeyboard;
bleKeyboardInstance->connectionStatus->outputKeyboard = bleKeyboardInstance->outputKeyboard;

bleKeyboardInstance->outputKeyboard->setCallbacks(new KeyboardOutputCallbacks());

bleKeyboardInstance->hid->manufacturer()->setValue(bleKeyboardInstance->deviceManufacturer);

bleKeyboardInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
bleKeyboardInstance->hid->hidInfo(0x00,0x01);

BLESecurity *pSecurity = new BLESecurity();
//must be called before begin in order to set the name
void BleKeyboard::setName(std::string deviceName) {
this->deviceName = deviceName;
}

pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
/**
* @brief Sets the waiting time (in milliseconds) between multiple keystrokes in NimBLE mode.
*
* @param ms Time in milliseconds
*/
void BleKeyboard::setDelay(uint32_t ms) {
this->_delay_ms = ms;
}

bleKeyboardInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
bleKeyboardInstance->hid->startServices();
void BleKeyboard::set_vendor_id(uint16_t vid) {
this->vid = vid;
}

BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->setAppearance(HID_KEYBOARD);
pAdvertising->addServiceUUID(bleKeyboardInstance->hid->hidService()->getUUID());
pAdvertising->start();
bleKeyboardInstance->hid->setBatteryLevel(bleKeyboardInstance->batteryLevel);
void BleKeyboard::set_product_id(uint16_t pid) {
this->pid = pid;
}

ESP_LOGD(LOG_TAG, "Advertising started!");
vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
void BleKeyboard::set_version(uint16_t version) {
this->version = version;
}

void BleKeyboard::sendReport(KeyReport* keys)
Expand All @@ -159,7 +192,11 @@ void BleKeyboard::sendReport(KeyReport* keys)
{
this->inputKeyboard->setValue((uint8_t*)keys, sizeof(KeyReport));
this->inputKeyboard->notify();
}
#if defined(USE_NIMBLE)
// vTaskDelay(delayTicks);
this->delay_ms(_delay_ms);
#endif // USE_NIMBLE
}
}

void BleKeyboard::sendReport(MediaKeyReport* keys)
Expand All @@ -168,7 +205,11 @@ void BleKeyboard::sendReport(MediaKeyReport* keys)
{
this->inputMediaKeys->setValue((uint8_t*)keys, sizeof(MediaKeyReport));
this->inputMediaKeys->notify();
}
#if defined(USE_NIMBLE)
//vTaskDelay(delayTicks);
this->delay_ms(_delay_ms);
#endif // USE_NIMBLE
}
}

extern
Expand Down Expand Up @@ -427,6 +468,7 @@ void BleKeyboard::releaseAll(void)
_mediaKeyReport[0] = 0;
_mediaKeyReport[1] = 0;
sendReport(&_keyReport);
sendReport(&_mediaKeyReport);
}

size_t BleKeyboard::write(uint8_t c)
Expand Down Expand Up @@ -457,3 +499,49 @@ size_t BleKeyboard::write(const uint8_t *buffer, size_t size) {
}
return n;
}

void BleKeyboard::onConnect(BLEServer* pServer) {
this->connected = true;

#if !defined(USE_NIMBLE)

BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(true);
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(true);

#endif // !USE_NIMBLE

}

void BleKeyboard::onDisconnect(BLEServer* pServer) {
this->connected = false;

#if !defined(USE_NIMBLE)

BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(false);
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(false);

advertising->start();

#endif // !USE_NIMBLE
}

void BleKeyboard::onWrite(BLECharacteristic* me) {
uint8_t* value = (uint8_t*)(me->getValue().c_str());
(void)value;
ESP_LOGI(LOG_TAG, "special keys: %d", *value);
}

void BleKeyboard::delay_ms(uint64_t ms) {
uint64_t m = esp_timer_get_time();
if(ms){
uint64_t e = (m + (ms * 1000));
if(m > e){ //overflow
while(esp_timer_get_time() > e) { }
}
while(esp_timer_get_time() < e) {}
}
}
Loading