#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "BLEHIDDevice.h"
#include "BLE2904.h"
BLEHIDDevice::BLEHIDDevice(BLEServer* server) {
m_deviceInfoService = server->createService(BLEUUID((uint16_t) 0x180a));
m_hidService = server->createService(BLEUUID((uint16_t) 0x1812), 40);
m_batteryService = server->createService(BLEUUID((uint16_t) 0x180f));
m_pnpCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a50, BLECharacteristic::PROPERTY_READ);
m_hidInfoCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4a, BLECharacteristic::PROPERTY_READ);
m_reportMapCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4b, BLECharacteristic::PROPERTY_READ);
m_hidControlCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4c, BLECharacteristic::PROPERTY_WRITE_NR);
m_protocolModeCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4e, BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_READ);
BLE2904* batteryLevelDescriptor = new BLE2904();
batteryLevelDescriptor->setFormat(BLE2904::FORMAT_UINT8);
batteryLevelDescriptor->setNamespace(1);
batteryLevelDescriptor->setUnit(0x27ad);
m_batteryLevelCharacteristic = m_batteryService->createCharacteristic((uint16_t)0x2a19, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
m_batteryLevelCharacteristic->addDescriptor(batteryLevelDescriptor);
m_batteryLevelCharacteristic->addDescriptor(new BLE2902());
const uint8_t pMode[] = {0x01};
protocolMode()->setValue((uint8_t*)pMode, 1);
}
BLEHIDDevice::~BLEHIDDevice() {
}
void BLEHIDDevice::reportMap(uint8_t* map, uint16_t size) {
m_reportMapCharacteristic->setValue(map, size);
}
void BLEHIDDevice::startServices() {
m_deviceInfoService->start();
m_hidService->start();
m_batteryService->start();
}
BLECharacteristic* BLEHIDDevice::manufacturer() {
m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a29, BLECharacteristic::PROPERTY_READ);
return m_manufacturerCharacteristic;
}
void BLEHIDDevice::manufacturer(std::string name) {
m_manufacturerCharacteristic->setValue(name);
}
void BLEHIDDevice::pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version) {
uint8_t pnp[] = {sig, (uint8_t)(vid>>8), (uint8_t)vid, (uint8_t)(pid>>8), (uint8_t)pid, (uint8_t)(version>>8), (uint8_t)version};
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
}
void BLEHIDDevice::hidInfo(uint8_t country, uint8_t flags) {
uint8_t info[] = {0x11,0x1, country, flags};
m_hidInfoCharacteristic->setValue(info, sizeof(info));;
}
BLECharacteristic* BLEHIDDevice::inputReport(uint8_t reportID) {
BLECharacteristic* inputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor* inputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
uint8_t desc1_val[] = {reportID, 0x01};
inputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
inputReportCharacteristic->addDescriptor(new BLE2902());
inputReportCharacteristic->addDescriptor(inputReportDescriptor);
return inputReportCharacteristic;
}
BLECharacteristic* BLEHIDDevice::outputReport(uint8_t reportID) {
BLECharacteristic* outputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
BLEDescriptor* outputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
uint8_t desc1_val[] = {reportID, 0x02};
outputReportDescriptor->setValue((uint8_t*)desc1_val, 2);
outputReportCharacteristic->addDescriptor(outputReportDescriptor);
return outputReportCharacteristic;
}
BLECharacteristic* BLEHIDDevice::featureReport(uint8_t reportID) {
BLECharacteristic* featureReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
BLEDescriptor* featureReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
uint8_t desc1_val[] = {reportID, 0x03};
featureReportDescriptor->setValue((uint8_t*)desc1_val, 2);
featureReportCharacteristic->addDescriptor(featureReportDescriptor);
return featureReportCharacteristic;
}
BLECharacteristic* BLEHIDDevice::bootInput() {
BLECharacteristic* bootInputCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a22, BLECharacteristic::PROPERTY_NOTIFY);
bootInputCharacteristic->addDescriptor(new BLE2902());
return bootInputCharacteristic;
}
BLECharacteristic* BLEHIDDevice::bootOutput() {
return m_hidService->createCharacteristic((uint16_t)0x2a32, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
}
BLECharacteristic* BLEHIDDevice::hidControl() {
return m_hidControlCharacteristic;
}
BLECharacteristic* BLEHIDDevice::protocolMode() {
return m_protocolModeCharacteristic;
}
void BLEHIDDevice::setBatteryLevel(uint8_t level) {
m_batteryLevelCharacteristic->setValue(&level, 1);
}
BLEService* BLEHIDDevice::deviceInfo() {
return m_deviceInfoService;
}
BLEService* BLEHIDDevice::hidService() {
return m_hidService;
}
BLEService* BLEHIDDevice::batteryService() {
return m_batteryService;
}
#endif