-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathBleOutputReceiver.cpp
More file actions
41 lines (35 loc) · 1.13 KB
/
Copy pathBleOutputReceiver.cpp
File metadata and controls
41 lines (35 loc) · 1.13 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
#include "BleOutputReceiver.h"
BleOutputReceiver::BleOutputReceiver(uint16_t outputReportLength)
{
this->outputReportLength = outputReportLength;
outputBuffer = new uint8_t[outputReportLength];
}
BleOutputReceiver::~BleOutputReceiver()
{
// Release memory
if (outputBuffer)
{
delete[] outputBuffer;
}
}
void BleOutputReceiver::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
// Output 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)outputReportLength + 1)
{
data++;
length--;
}
// Store the received data in the buffer
for (size_t i = 0; i < std::min(length, (size_t)outputReportLength); i++)
{
outputBuffer[i] = data[i];
}
outputFlag = true;
}