2020-10-14 03:52:52 +02: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-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
#include "NimBLEHIDDevice.h"
|
|
|
|
#include "NimBLE2904.h"
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Construct a default NimBLEHIDDevice object.
|
|
|
|
* @param [in] server A pointer to the server instance this HID Device will use.
|
|
|
|
*/
|
2020-10-14 03:52:52 +02:00
|
|
|
NimBLEHIDDevice::NimBLEHIDDevice(NimBLEServer* server) {
|
|
|
|
/*
|
|
|
|
* Here we create mandatory services described in bluetooth specification
|
|
|
|
*/
|
2024-06-03 00:40:32 +02:00
|
|
|
m_deviceInfoService = server->createService(NimBLEUUID((uint16_t)0x180a));
|
|
|
|
m_hidService = server->createService(NimBLEUUID((uint16_t)0x1812));
|
|
|
|
m_batteryService = server->createService(NimBLEUUID((uint16_t)0x180f));
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mandatory characteristic for device info service
|
|
|
|
*/
|
2024-06-03 00:40:32 +02:00
|
|
|
m_pnpCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a50, NIMBLE_PROPERTY::READ);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
2023-03-25 12:49:37 +01:00
|
|
|
/*
|
|
|
|
* Non-mandatory characteristics for device info service
|
|
|
|
* Will be created on demand
|
|
|
|
*/
|
|
|
|
m_manufacturerCharacteristic = nullptr;
|
|
|
|
|
2020-10-14 03:52:52 +02:00
|
|
|
/*
|
|
|
|
* Mandatory characteristics for HID service
|
|
|
|
*/
|
2024-06-03 00:40:32 +02:00
|
|
|
m_hidInfoCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4a, NIMBLE_PROPERTY::READ);
|
|
|
|
m_reportMapCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4b, NIMBLE_PROPERTY::READ);
|
|
|
|
m_hidControlCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4c, NIMBLE_PROPERTY::WRITE_NR);
|
|
|
|
m_protocolModeCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4e, NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::READ);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mandatory battery level characteristic with notification and presence descriptor
|
|
|
|
*/
|
2024-06-03 00:40:32 +02:00
|
|
|
m_batteryLevelCharacteristic = m_batteryService->createCharacteristic((uint16_t)0x2a19, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
|
|
|
|
NimBLE2904 *batteryLevelDescriptor = (NimBLE2904*)m_batteryLevelCharacteristic->createDescriptor((uint16_t)0x2904);
|
2020-10-14 03:52:52 +02:00
|
|
|
batteryLevelDescriptor->setFormat(NimBLE2904::FORMAT_UINT8);
|
|
|
|
batteryLevelDescriptor->setNamespace(1);
|
|
|
|
batteryLevelDescriptor->setUnit(0x27ad);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This value is setup here because its default value in most usage cases, its very rare to use boot mode
|
|
|
|
* and we want to simplify library using as much as possible
|
|
|
|
*/
|
2024-06-03 00:40:32 +02:00
|
|
|
const uint8_t pMode[] = {0x01};
|
|
|
|
protocolMode()->setValue((uint8_t*)pMode, 1);
|
2020-10-14 03:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NimBLEHIDDevice::~NimBLEHIDDevice() {
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01: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-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::reportMap(uint8_t* map, uint16_t size) {
|
|
|
|
m_reportMapCharacteristic->setValue(map, size);
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Start the HID device services.\n
|
|
|
|
* This function called when all the services have been created.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::startServices() {
|
|
|
|
m_deviceInfoService->start();
|
|
|
|
m_hidService->start();
|
|
|
|
m_batteryService->start();
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Create a manufacturer characteristic (this characteristic is optional).
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::manufacturer() {
|
2023-03-25 12:49:37 +01:00
|
|
|
if (m_manufacturerCharacteristic == nullptr) {
|
|
|
|
m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a29, NIMBLE_PROPERTY::READ);
|
|
|
|
}
|
|
|
|
|
2020-10-14 03:52:52 +02:00
|
|
|
return m_manufacturerCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
2020-10-14 03:52:52 +02:00
|
|
|
* @brief Set manufacturer name
|
2021-01-17 18:19:58 +01:00
|
|
|
* @param [in] name The manufacturer name of this HID device.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::manufacturer(std::string name) {
|
2023-03-25 12:49:37 +01:00
|
|
|
manufacturer()->setValue(name);
|
2020-10-14 03:52:52 +02:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
2022-07-31 19:00:12 +02:00
|
|
|
* @brief Sets the Plug n Play characteristic value.
|
2021-01-17 18:19:58 +01:00
|
|
|
* @param [in] sig The vendor ID source number.
|
2021-02-06 19:12:40 +01:00
|
|
|
* @param [in] vid The vendor ID number.
|
2021-01-17 18:19:58 +01:00
|
|
|
* @param [in] pid The product ID number.
|
|
|
|
* @param [in] version The produce version number.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version) {
|
2023-03-25 12:49:37 +01:00
|
|
|
uint8_t pnp[] = {
|
|
|
|
sig,
|
2024-06-03 00:40:32 +02:00
|
|
|
((uint8_t*)&vid)[0],
|
|
|
|
((uint8_t*)&vid)[1],
|
|
|
|
((uint8_t*)&pid)[0],
|
|
|
|
((uint8_t*)&pid)[1],
|
|
|
|
((uint8_t*)&version)[0],
|
|
|
|
((uint8_t*)&version)[1]
|
2023-03-25 12:49:37 +01:00
|
|
|
};
|
2020-10-14 03:52:52 +02:00
|
|
|
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01: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-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
void NimBLEHIDDevice::hidInfo(uint8_t country, uint8_t flags) {
|
2024-06-03 00:40:32 +02:00
|
|
|
uint8_t info[] = {0x11, 0x1, country, flags};
|
2020-10-14 03:52:52 +02:00
|
|
|
m_hidInfoCharacteristic->setValue(info, sizeof(info));
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Create input report characteristic
|
|
|
|
* @param [in] reportID input report ID, the same as in report map for input object related to the characteristic
|
2020-10-14 03:52:52 +02:00
|
|
|
* @return pointer to new input report characteristic
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::inputReport(uint8_t reportID) {
|
2024-06-03 00:40:32 +02:00
|
|
|
NimBLECharacteristic *inputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ_ENC);
|
|
|
|
NimBLEDescriptor *inputReportDescriptor = inputReportCharacteristic->createDescriptor((uint16_t)0x2908, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
2024-06-03 00:40:32 +02:00
|
|
|
uint8_t desc1_val[] = {reportID, 0x01};
|
|
|
|
inputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
return inputReportCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Create output report characteristic
|
|
|
|
* @param [in] reportID Output report ID, the same as in report map for output object related to the characteristic
|
2020-10-14 03:52:52 +02:00
|
|
|
* @return Pointer to new output report characteristic
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::outputReport(uint8_t reportID) {
|
2024-06-03 00:40:32 +02:00
|
|
|
NimBLECharacteristic *outputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
NimBLEDescriptor *outputReportDescriptor = outputReportCharacteristic->createDescriptor((uint16_t)0x2908, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
2024-06-03 00:40:32 +02:00
|
|
|
uint8_t desc1_val[] = {reportID, 0x02};
|
|
|
|
outputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
return outputReportCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Create feature report characteristic.
|
|
|
|
* @param [in] reportID Feature report ID, the same as in report map for feature object related to the characteristic
|
2020-10-14 03:52:52 +02:00
|
|
|
* @return Pointer to new feature report characteristic
|
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::featureReport(uint8_t reportID) {
|
2024-06-03 00:40:32 +02:00
|
|
|
NimBLECharacteristic *featureReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
|
|
|
NimBLEDescriptor *featureReportDescriptor = featureReportCharacteristic->createDescriptor((uint16_t)0x2908, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
2024-06-03 00:40:32 +02:00
|
|
|
uint8_t desc1_val[] = {reportID, 0x03};
|
|
|
|
featureReportDescriptor->setValue((uint8_t*)desc1_val, 2);
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
return featureReportCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Creates a keyboard boot input report characteristic
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::bootInput() {
|
2024-06-03 00:40:32 +02:00
|
|
|
return m_hidService->createCharacteristic((uint16_t)0x2a22, NIMBLE_PROPERTY::NOTIFY);
|
2020-10-14 03:52:52 +02:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Create a keyboard boot output report characteristic
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::bootOutput() {
|
2024-06-03 00:40:32 +02:00
|
|
|
return m_hidService->createCharacteristic((uint16_t)0x2a32, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
|
2020-10-14 03:52:52 +02:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the HID control point characteristic.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::hidControl() {
|
|
|
|
return m_hidControlCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the protocol mode characteristic.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::protocolMode() {
|
|
|
|
return m_protocolModeCharacteristic;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Set the battery level characteristic value.
|
|
|
|
* @param [in] level The battery level value.
|
|
|
|
*/
|
2020-10-14 03:52:52 +02:00
|
|
|
void NimBLEHIDDevice::setBatteryLevel(uint8_t level) {
|
|
|
|
m_batteryLevelCharacteristic->setValue(&level, 1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* @brief Returns battery level characteristic
|
|
|
|
* @ return battery level characteristic
|
2022-07-24 16:34:33 +02:00
|
|
|
*/
|
|
|
|
NimBLECharacteristic* NimBLEHIDDevice::batteryLevel() {
|
2020-10-14 03:52:52 +02:00
|
|
|
return m_batteryLevelCharacteristic;
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:34:33 +02:00
|
|
|
/*
|
2020-10-14 03:52:52 +02:00
|
|
|
|
|
|
|
BLECharacteristic* BLEHIDDevice::reportMap() {
|
|
|
|
return m_reportMapCharacteristic;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLECharacteristic* BLEHIDDevice::pnp() {
|
|
|
|
return m_pnpCharacteristic;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BLECharacteristic* BLEHIDDevice::hidInfo() {
|
|
|
|
return m_hidInfoCharacteristic;
|
|
|
|
}
|
|
|
|
*/
|
2021-01-17 18:19:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the device information service.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLEService* NimBLEHIDDevice::deviceInfo() {
|
|
|
|
return m_deviceInfoService;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the HID service.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLEService* NimBLEHIDDevice::hidService() {
|
|
|
|
return m_hidService;
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:19:58 +01:00
|
|
|
/**
|
|
|
|
* @brief @brief Returns a pointer to the battery service.
|
2020-10-14 03:52:52 +02:00
|
|
|
*/
|
|
|
|
NimBLEService* NimBLEHIDDevice::batteryService() {
|
|
|
|
return m_batteryService;
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
|