-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathBleFeatureReport.cpp
More file actions
46 lines (39 loc) · 1.31 KB
/
Copy pathBleFeatureReport.cpp
File metadata and controls
46 lines (39 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "BleFeatureReport.h"
BleFeatureReceiver::BleFeatureReceiver(uint16_t featureReportLength)
{
this->featureReportLength = featureReportLength;
featureBuffer = new uint8_t[featureReportLength];
}
BleFeatureReceiver::~BleFeatureReceiver()
{
// Release memory
if (featureBuffer)
{
delete[] featureBuffer;
}
}
void BleFeatureReceiver::onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo& connInfo)
{
// Set data for the host
pCharacteristic->setValue(featureBuffer, featureReportLength);
}
void BleFeatureReceiver::onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo& connInfo)
{
// Retrieve data sent from the host
std::string value = pCharacteristic->getValue();
// Some hosts (e.g. macOS's BLE HID bridge) prepend the Report ID byte to
// Feature Report writes even though the GATT characteristic (and its Report
// Reference descriptor) already identifies the report; strip it if present.
const uint8_t* data = (const uint8_t*)value.c_str();
size_t length = value.length();
if (length == (size_t)featureReportLength + 1)
{
data++;
length--;
}
if (length <= featureReportLength && memcmp(data, featureBuffer, length) != 0)
{
memcpy(featureBuffer, data, length);
}
featureFlag = true;
}