This commit is contained in:
h2zero 2024-11-19 14:14:14 -07:00
parent 5f2730de02
commit 198b045380
2 changed files with 155 additions and 149 deletions

View file

@ -47,12 +47,14 @@ NimBLEHIDDevice::NimBLEHIDDevice(NimBLEServer* server) {
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);
m_protocolModeCharacteristic =
m_hidService->createCharacteristic((uint16_t)0x2a4e, NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::READ);
/*
* Mandatory battery level characteristic with notification and presence descriptor
*/
m_batteryLevelCharacteristic = m_batteryService->createCharacteristic((uint16_t)0x2a19, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
m_batteryLevelCharacteristic =
m_batteryService->createCharacteristic((uint16_t)0x2a19, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
NimBLE2904* batteryLevelDescriptor = (NimBLE2904*)m_batteryLevelCharacteristic->createDescriptor((uint16_t)0x2904);
batteryLevelDescriptor->setFormat(NimBLE2904::FORMAT_UINT8);
batteryLevelDescriptor->setNamespace(1);
@ -62,11 +64,8 @@ NimBLEHIDDevice::NimBLEHIDDevice(NimBLEServer* server) {
* 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
*/
const uint8_t pMode[] = {0x01};
protocolMode()->setValue((uint8_t*)pMode, 1);
}
NimBLEHIDDevice::~NimBLEHIDDevice() {
const uint8_t pMode{0x01};
protocolMode()->setValue(pMode);
}
/**
@ -103,7 +102,7 @@ NimBLECharacteristic* NimBLEHIDDevice::manufacturer() {
* @brief Set manufacturer name
* @param [in] name The manufacturer name of this HID device.
*/
void NimBLEHIDDevice::manufacturer(std::string name) {
void NimBLEHIDDevice::manufacturer(const std::string& name) {
manufacturer()->setValue(name);
}
@ -115,15 +114,13 @@ void NimBLEHIDDevice::manufacturer(std::string name) {
* @param [in] version The produce version number.
*/
void NimBLEHIDDevice::pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version) {
uint8_t pnp[] = {
sig,
uint8_t pnp[] = {sig,
((uint8_t*)&vid)[0],
((uint8_t*)&vid)[1],
((uint8_t*)&pid)[0],
((uint8_t*)&pid)[1],
((uint8_t*)&version)[0],
((uint8_t*)&version)[1]
};
((uint8_t*)&version)[1]};
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
}
@ -143,8 +140,11 @@ void NimBLEHIDDevice::hidInfo(uint8_t country, uint8_t flags) {
* @return pointer to new input report characteristic
*/
NimBLECharacteristic* NimBLEHIDDevice::inputReport(uint8_t reportID) {
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);
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);
uint8_t desc1_val[] = {reportID, 0x01};
inputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
@ -158,8 +158,13 @@ NimBLECharacteristic* NimBLEHIDDevice::inputReport(uint8_t reportID) {
* @return Pointer to new output report characteristic
*/
NimBLECharacteristic* NimBLEHIDDevice::outputReport(uint8_t reportID) {
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);
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);
uint8_t desc1_val[] = {reportID, 0x02};
outputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
@ -173,8 +178,12 @@ NimBLECharacteristic* NimBLEHIDDevice::outputReport(uint8_t reportID) {
* @return Pointer to new feature report characteristic
*/
NimBLECharacteristic* NimBLEHIDDevice::featureReport(uint8_t reportID) {
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);
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);
uint8_t desc1_val[] = {reportID, 0x03};
featureReportDescriptor->setValue((uint8_t*)desc1_val, 2);
@ -193,7 +202,8 @@ NimBLECharacteristic* NimBLEHIDDevice::bootInput() {
* @brief Create a keyboard boot output report characteristic
*/
NimBLECharacteristic* NimBLEHIDDevice::bootOutput() {
return m_hidService->createCharacteristic((uint16_t)0x2a32, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
return m_hidService->createCharacteristic((uint16_t)0x2a32,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
}
/**

View file

@ -12,16 +12,14 @@
* Author: chegewara
*/
#ifndef _BLEHIDDEVICE_H_
#define _BLEHIDDEVICE_H_
#ifndef NIMBLE_CPP_HIDDEVICE_H_
#define NIMBLE_CPP_HIDDEVICE_H_
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h"
#include "NimBLEService.h"
#include "NimBLEDescriptor.h"
#include "HIDTypes.h"
#include <stdint.h>
#include <string>
# define GENERIC_HID 0x03C0
# define HID_KEYBOARD 0x03C1
@ -33,33 +31,31 @@
# define HID_DIGITAL_PEN 0x03C7
# define HID_BARCODE 0x03C8
#define PNPVersionField(MajorVersion, MinorVersion, PatchVersion) ((MajorVersion << 16) & 0xFF00) | ((MinorVersion << 8) & 0x00F0) | (PatchVersion & 0x000F)
# define PNPVersionField(MajorVersion, MinorVersion, PatchVersion) \
((MajorVersion << 16) & 0xFF00) | ((MinorVersion << 8) & 0x00F0) | (PatchVersion & 0x000F)
class NimBLEServer;
class NimBLEService;
class NimBLECharacteristic;
/**
* @brief A model of a %BLE Human Interface Device.
* @brief A model of a BLE Human Interface Device.
*/
class NimBLEHIDDevice {
public:
NimBLEHIDDevice(NimBLEServer*);
virtual ~NimBLEHIDDevice();
NimBLEHIDDevice(NimBLEServer* server);
void reportMap(uint8_t* map, uint16_t);
void startServices();
NimBLEService* deviceInfo();
NimBLEService* hidService();
NimBLEService* batteryService();
NimBLECharacteristic* manufacturer();
void manufacturer(std::string name);
//NimBLECharacteristic* pnp();
void manufacturer(const std::string& name);
void pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version);
//NimBLECharacteristic* hidInfo();
void hidInfo(uint8_t country, uint8_t flags);
NimBLECharacteristic* batteryLevel();
void setBatteryLevel(uint8_t level);
NimBLECharacteristic* batteryLevel();
NimBLECharacteristic* reportMap();
NimBLECharacteristic* hidControl();
NimBLECharacteristic* inputReport(uint8_t reportID);
@ -83,5 +79,5 @@ private:
NimBLECharacteristic* m_batteryLevelCharacteristic; // 0x2a19
};
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER */
#endif /* _BLEHIDDEVICE_H_ */
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) */
#endif /* NIMBLE_CPP_HIDDEVICE_H_ */