forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEHIDPeripheral.cpp
82 lines (60 loc) · 2.13 KB
/
BLEHIDPeripheral.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "BLEHIDPeripheral.h"
static const PROGMEM unsigned char hidInformationCharacteriticValue[] = { 0x11, 0x01, 0x00, 0x03 };
BLEHIDPeripheral* BLEHIDPeripheral::_instance = NULL;
BLEHIDPeripheral::BLEHIDPeripheral(BLE_Tx_Power_Level tx_power_level, unsigned char req, unsigned char rdy, unsigned char rst) :
BLEPeripheral(tx_power_level, req, rdy, rst),
_bleBondStore(),
_hidService("1812"),
_hidInformationCharacteristic("2a4a", hidInformationCharacteriticValue, sizeof(hidInformationCharacteriticValue)),
_hidControlPointCharacteristic("2a4c", BLEWriteWithoutResponse),
_hidReportMapCharacteristic(),
_reportIdOffset(0),
_hids(NULL),
_numHids(0)
{
_instance = this;
}
BLEHIDPeripheral::~BLEHIDPeripheral() {
if (this->_hids) {
free(this->_hids);
}
}
BLEHIDPeripheral* BLEHIDPeripheral::instance() {
return _instance;
}
void BLEHIDPeripheral::begin() {
this->setBondStore(this->_bleBondStore);
this->setAdvertisedServiceUuid(this->_hidService.uuid());
this->addAttribute(this->_hidService);
this->addAttribute(this->_hidInformationCharacteristic);
this->addAttribute(this->_hidControlPointCharacteristic);
this->addAttribute(this->_hidReportMapCharacteristic);
for (int i = 0; i < this->_numHids; i++) {
BLEHID *hid = this->_hids[i];
unsigned char numAttributes = hid->numAttributes();
BLELocalAttribute** attributes = hid->attributes();
for (int j = 0; j < numAttributes; j++) {
this->addAttribute(*attributes[j]);
}
}
this->_hidReportMapCharacteristic.setHids(this->_hids, this->_numHids);
// begin initialization
BLEPeripheral::begin();
}
void BLEHIDPeripheral::clearBondStoreData() {
this->_bleBondStore.clearData();
}
void BLEHIDPeripheral::setReportIdOffset(unsigned char reportIdOffset) {
this->_reportIdOffset = reportIdOffset;
}
void BLEHIDPeripheral::poll() {
BLEPeripheral::poll();
}
void BLEHIDPeripheral::addHID(BLEHID& hid) {
if (this->_hids == NULL) {
this->_hids = (BLEHID**)malloc(sizeof(BLEHID*) * BLEHID::numHids());
}
hid.setReportId(this->_numHids + this->_reportIdOffset);
this->_hids[this->_numHids] = &hid;
this->_numHids++;
}