2020-10-13 19:52:52 -06:00
|
|
|
/*
|
|
|
|
* NimBLEHIDDevice.cpp
|
|
|
|
*
|
|
|
|
* Created: on Oct 06 2020
|
|
|
|
* Author wakwak-koba
|
|
|
|
*
|
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEHIDDevice.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jan 03, 2018
|
|
|
|
* Author: chegewara
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nimconfig.h"
|
2021-09-06 21:14:43 -06:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
# include "NimBLEHIDDevice.h"
|
|
|
|
# include "NimBLEServer.h"
|
|
|
|
# include "NimBLEService.h"
|
|
|
|
# include "NimBLE2904.h"
|
|
|
|
|
|
|
|
static constexpr uint16_t deviceInfoSvcUuid = 0x180a;
|
|
|
|
static constexpr uint16_t hidSvcUuid = 0x1812;
|
|
|
|
static constexpr uint16_t batterySvcUuid = 0x180f;
|
|
|
|
|
|
|
|
static constexpr uint16_t pnpCharUuid = 0x2a50;
|
|
|
|
static constexpr uint16_t hidInfoCharUuid = 0x2a4a;
|
|
|
|
static constexpr uint16_t reportMapCharUuid = 0x2a4b;
|
|
|
|
static constexpr uint16_t hidControlCharUuid = 0x2a4c;
|
|
|
|
static constexpr uint16_t inputReportChrUuid = 0x2a4d;
|
|
|
|
static constexpr uint16_t protocolModeCharUuid = 0x2a4e;
|
|
|
|
static constexpr uint16_t batteryLevelCharUuid = 0x2a19;
|
|
|
|
static constexpr uint16_t batteryLevelDscUuid = 0x2904;
|
|
|
|
static constexpr uint16_t featureReportDscUuid = 0x2908;
|
|
|
|
static constexpr uint16_t m_manufacturerChrUuid = 0x2a29;
|
|
|
|
static constexpr uint16_t bootInputChrUuid = 0x2a22;
|
|
|
|
static constexpr uint16_t bootOutputChrUuid = 0x2a32;
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
|
|
|
* @brief Construct a default NimBLEHIDDevice object.
|
|
|
|
* @param [in] server A pointer to the server instance this HID Device will use.
|
|
|
|
*/
|
2020-10-13 19:52:52 -06:00
|
|
|
NimBLEHIDDevice::NimBLEHIDDevice(NimBLEServer* server) {
|
2024-11-19 14:14:14 -07:00
|
|
|
// Here we create mandatory services described in bluetooth specification
|
|
|
|
m_deviceInfoSvc = server->createService(deviceInfoSvcUuid);
|
|
|
|
m_hidSvc = server->createService(hidSvcUuid);
|
|
|
|
m_batterySvc = server->createService(batterySvcUuid);
|
|
|
|
|
|
|
|
// Mandatory characteristic for device info service
|
|
|
|
m_pnpChr = m_deviceInfoSvc->createCharacteristic(pnpCharUuid, NIMBLE_PROPERTY::READ);
|
|
|
|
|
|
|
|
// Mandatory characteristics for HID service
|
|
|
|
m_hidInfoChr = m_hidSvc->createCharacteristic(hidInfoCharUuid, NIMBLE_PROPERTY::READ);
|
|
|
|
m_reportMapChr = m_hidSvc->createCharacteristic(reportMapCharUuid, NIMBLE_PROPERTY::READ);
|
|
|
|
m_hidControlChr = m_hidSvc->createCharacteristic(hidControlCharUuid, NIMBLE_PROPERTY::WRITE_NR);
|
|
|
|
m_protocolModeChr =
|
|
|
|
m_hidSvc->createCharacteristic(protocolModeCharUuid, NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::READ);
|
|
|
|
|
|
|
|
// Mandatory battery level characteristic with notification and presence descriptor
|
|
|
|
m_batteryLevelChr =
|
|
|
|
m_batterySvc->createCharacteristic(batteryLevelCharUuid, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
|
|
|
|
NimBLE2904* batteryLevelDescriptor = m_batteryLevelChr->create2904();
|
|
|
|
batteryLevelDescriptor->setFormat(NimBLE2904::FORMAT_UINT8);
|
|
|
|
batteryLevelDescriptor->setUnit(0x27ad); // percentage
|
|
|
|
|
|
|
|
// This value is setup here because its default value in most usage cases, it's very rare to use boot mode
|
|
|
|
m_protocolModeChr->setValue(static_cast<uint8_t>(0x01));
|
|
|
|
} // NimBLEHIDDevice
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
|
|
|
* @brief Set the report map data formatting information.
|
|
|
|
* @param [in] map A pointer to an array with the values to set.
|
|
|
|
* @param [in] size The number of values in the array.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
void NimBLEHIDDevice::setReportMap(uint8_t* map, uint16_t size) {
|
|
|
|
m_reportMapChr->setValue(map, size);
|
|
|
|
} // setReportMap
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Start the HID device services.
|
2021-01-17 10:19:58 -07:00
|
|
|
* This function called when all the services have been created.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::startServices() {
|
2024-11-19 14:14:14 -07:00
|
|
|
m_deviceInfoSvc->start();
|
|
|
|
m_hidSvc->start();
|
|
|
|
m_batterySvc->start();
|
|
|
|
} // startServices
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the manufacturer characteristic (this characteristic is optional).
|
|
|
|
* @details The characteristic will be created if not already existing.
|
|
|
|
* @returns True if the name was set and/or the characteristic created.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
bool NimBLEHIDDevice::setManufacturer(const std::string& name) {
|
|
|
|
if (m_manufacturerChr == nullptr) {
|
|
|
|
m_manufacturerChr = m_deviceInfoSvc->createCharacteristic(m_manufacturerChrUuid, NIMBLE_PROPERTY::READ);
|
|
|
|
}
|
2023-03-25 12:49:37 +01:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
if (m_manufacturerChr) {
|
|
|
|
m_manufacturerChr->setValue(name);
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
return false;
|
|
|
|
} // setManufacturer
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2022-07-31 11:00:12 -06:00
|
|
|
* @brief Sets the Plug n Play characteristic value.
|
2021-01-17 10:19:58 -07:00
|
|
|
* @param [in] sig The vendor ID source number.
|
2021-02-06 11:12:40 -07:00
|
|
|
* @param [in] vid The vendor ID number.
|
2021-01-17 10:19:58 -07:00
|
|
|
* @param [in] pid The product ID number.
|
|
|
|
* @param [in] version The produce version number.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
void NimBLEHIDDevice::setPnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version) {
|
|
|
|
uint8_t pnp[] = {sig,
|
|
|
|
static_cast<uint8_t>(vid & 0xFF),
|
|
|
|
static_cast<uint8_t>((vid >> 8) & 0xFF),
|
|
|
|
static_cast<uint8_t>(pid & 0xFF),
|
|
|
|
static_cast<uint8_t>((pid >> 8) & 0xFF),
|
|
|
|
static_cast<uint8_t>(version & 0xFF),
|
|
|
|
static_cast<uint8_t>((version >> 8) & 0xFF)};
|
|
|
|
|
|
|
|
m_pnpChr->setValue(pnp, sizeof(pnp));
|
|
|
|
} // setPnp
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
|
|
|
* @brief Sets the HID Information characteristic value.
|
|
|
|
* @param [in] country The country code for the device.
|
|
|
|
* @param [in] flags The HID Class Specification release number to use.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
void NimBLEHIDDevice::setHidInfo(uint8_t country, uint8_t flags) {
|
|
|
|
uint8_t info[] = {0x11, 0x1, country, flags};
|
|
|
|
m_hidInfoChr->setValue(info, sizeof(info));
|
|
|
|
} // setHidInfo
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Set the battery level characteristic value.
|
|
|
|
* @param [in] level The battery level value.
|
|
|
|
* @param [in] notify If true sends a notification to the peer device, otherwise not. default = false
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
void NimBLEHIDDevice::setBatteryLevel(uint8_t level, bool notify) {
|
|
|
|
m_batteryLevelChr->setValue(&level, 1);
|
|
|
|
if (notify) {
|
|
|
|
m_batteryLevelChr->notify();
|
|
|
|
}
|
|
|
|
} // setBatteryLevel
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the input report characteristic.
|
|
|
|
* @param [in] reportId input report ID, the same as in report map for input object related to the characteristic.
|
|
|
|
* @return A pointer to the input report characteristic.
|
|
|
|
* @details This will create the characteristic if not already created.
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getInputReport(uint8_t reportId) {
|
|
|
|
NimBLECharacteristic* inputReportChr = m_hidSvc->getCharacteristic(inputReportChrUuid);
|
|
|
|
if (inputReportChr == nullptr) {
|
|
|
|
inputReportChr =
|
|
|
|
m_hidSvc->createCharacteristic(inputReportChrUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ_ENC);
|
|
|
|
NimBLEDescriptor* inputReportDsc =
|
|
|
|
inputReportChr->createDescriptor(featureReportDscUuid, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC);
|
|
|
|
|
|
|
|
uint8_t desc1_val[] = {reportId, 0x01};
|
|
|
|
inputReportDsc->setValue(desc1_val, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputReportChr;
|
|
|
|
} // getInputReport
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the output report characteristic.
|
|
|
|
* @param [in] reportId Output report ID, the same as in report map for output object related to the characteristic.
|
|
|
|
* @return A pointer to the output report characteristic.
|
|
|
|
* @details This will create the characteristic if not already created.
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getOutputReport(uint8_t reportId) {
|
|
|
|
NimBLECharacteristic* outputReportChr = m_hidSvc->getCharacteristic(inputReportChrUuid);
|
|
|
|
if (outputReportChr == nullptr) {
|
|
|
|
outputReportChr =
|
|
|
|
m_hidSvc->createCharacteristic(inputReportChrUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR |
|
|
|
|
NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
NimBLEDescriptor* outputReportDsc = outputReportChr->createDescriptor(
|
|
|
|
featureReportDscUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
|
|
|
|
uint8_t desc1_val[] = {reportId, 0x02};
|
|
|
|
outputReportDsc->setValue(desc1_val, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputReportChr;
|
|
|
|
} // getOutputReport
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the feature report characteristic.
|
|
|
|
* @param [in] reportId Feature report ID, the same as in report map for feature object related to the characteristic.
|
|
|
|
* @return A pointer to feature report characteristic.
|
|
|
|
* @details This will create the characteristic if not already created.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getFeatureReport(uint8_t reportId) {
|
|
|
|
NimBLECharacteristic* featureReportChr = m_hidSvc->getCharacteristic(inputReportChrUuid);
|
|
|
|
if (featureReportChr == nullptr) {
|
|
|
|
featureReportChr = m_hidSvc->createCharacteristic(
|
|
|
|
inputReportChrUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
NimBLEDescriptor* featureReportDsc = featureReportChr->createDescriptor(
|
|
|
|
featureReportDscUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
|
|
|
|
uint8_t desc1_val[] = {reportId, 0x03};
|
|
|
|
featureReportDsc->setValue(desc1_val, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return featureReportChr;
|
|
|
|
} // getFeatureReport
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
/**
|
|
|
|
* @brief Get a keyboard boot input report characteristic.
|
|
|
|
* @returns A pointer to the boot input report characteristic, or nullptr on error.
|
|
|
|
* @details This will create the characteristic if not already created.
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getBootInput() {
|
|
|
|
NimBLECharacteristic* bootInputChr = m_hidSvc->getCharacteristic(bootInputChrUuid);
|
|
|
|
if (bootInputChr) {
|
|
|
|
return bootInputChr;
|
|
|
|
}
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
return m_hidSvc->createCharacteristic(bootInputChrUuid, NIMBLE_PROPERTY::NOTIFY);
|
|
|
|
} // getBootInput
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Create a keyboard boot output report characteristic
|
|
|
|
* @returns A pointer to the boot output report characteristic, or nullptr on error.
|
|
|
|
* @details This will create the characteristic if not already created.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getBootOutput() {
|
|
|
|
NimBLECharacteristic* bootOutputChr = m_hidSvc->getCharacteristic(bootOutputChrUuid);
|
|
|
|
if (bootOutputChr) {
|
|
|
|
return bootOutputChr;
|
|
|
|
}
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
return m_hidSvc->createCharacteristic(bootOutputChrUuid,
|
|
|
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
|
|
|
|
} // getBootOutput
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the HID control point characteristic.
|
|
|
|
* @returns A pointer to the HID control point characteristic.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getHidControl() {
|
|
|
|
return m_hidControlChr;
|
|
|
|
} // getHidControl
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the HID protocol mode characteristic.
|
|
|
|
* @returns a pointer to the protocol mode characteristic.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getProtocolMode() {
|
|
|
|
return m_protocolModeChr;
|
|
|
|
} // getProtocolMode
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the battery level characteristic
|
|
|
|
* @returns A pointer to the battery level characteristic
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getBatteryLevel() {
|
|
|
|
return m_batteryLevelChr;
|
|
|
|
} // getBatteryLevel
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the report map characteristic.
|
|
|
|
* @returns A pointer to the report map characteristic.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getReportMap() {
|
|
|
|
return m_reportMapChr;
|
|
|
|
} // getReportMap
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the PnP characteristic.
|
|
|
|
* @returns A pointer to the PnP characteristic.
|
2021-01-17 10:19:58 -07:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getPnp() {
|
|
|
|
return m_pnpChr;
|
|
|
|
} // getPnp
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2024-11-19 14:14:14 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the HID information characteristic.
|
|
|
|
* @returns A pointer to the HID information characteristic.
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::getHidInfo() {
|
|
|
|
return m_hidInfoChr;
|
|
|
|
} // hidInfo
|
2021-01-17 10:19:58 -07:00
|
|
|
|
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the manufacturer characteristic.
|
|
|
|
* @returns A pointer to the manufacturer characteristic.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLEService* NimBLEHIDDevice::getDeviceInfoService() {
|
|
|
|
return m_deviceInfoSvc;
|
|
|
|
} // getDeviceInfoService
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the HID service.
|
|
|
|
* @returns A pointer to the HID service.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLEService* NimBLEHIDDevice::getHidService() {
|
|
|
|
return m_hidSvc;
|
|
|
|
} // getHidService
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-01-17 10:19:58 -07:00
|
|
|
/**
|
2024-11-19 14:14:14 -07:00
|
|
|
* @brief Get the battery service.
|
|
|
|
* @returns A pointer to the battery service.
|
2020-10-13 19:52:52 -06:00
|
|
|
*/
|
2024-11-19 14:14:14 -07:00
|
|
|
NimBLEService* NimBLEHIDDevice::getBatteryService() {
|
|
|
|
return m_batterySvc;
|
|
|
|
} // getBatteryService
|
2020-10-13 19:52:52 -06:00
|
|
|
|
2021-09-06 21:14:43 -06:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
|