|
1 | 1 | /*
|
2 | 2 | * BLEHIDDevice.cpp
|
3 | 3 | *
|
4 |
| - * Created on: Dec 18, 2017 |
| 4 | + * Created on: Jan 03, 2018 |
5 | 5 | * Author: chegewara
|
6 | 6 | */
|
7 |
| - |
8 | 7 | #include "sdkconfig.h"
|
9 | 8 | #if defined(CONFIG_BT_ENABLED)
|
10 | 9 |
|
11 |
| -//#include "BLEUUID.h" |
12 | 10 | #include "BLEHIDDevice.h"
|
| 11 | +#include "BLE2904.h" |
| 12 | + |
13 | 13 |
|
14 | 14 | BLEHIDDevice::BLEHIDDevice(BLEServer* server) {
|
| 15 | + /* |
| 16 | + * Here we create mandatory services described in bluetooth specification |
| 17 | + */ |
15 | 18 | m_deviceInfoService = server->createService(BLEUUID((uint16_t) 0x180a));
|
16 | 19 | m_hidService = server->createService(BLEUUID((uint16_t) 0x1812), 40);
|
17 |
| - //m_batteryService = server->createService(BLEUUID((uint16_t) 0x180f)); |
18 |
| - createDescriptors(); |
19 |
| - createCharacteristics(); |
20 |
| -} |
| 20 | + m_batteryService = server->createService(BLEUUID((uint16_t) 0x180f)); |
21 | 21 |
|
22 |
| -BLEHIDDevice::~BLEHIDDevice() { |
23 |
| - // TODO Auto-generated destructor stub |
24 |
| -} |
25 |
| - |
26 |
| -void BLEHIDDevice::setReportMap(uint8_t* map, uint16_t size) { |
27 |
| - m_reportMapCharacteristic->setValue(map, size); |
28 |
| -} |
29 |
| - |
30 |
| -void BLEHIDDevice::createDescriptors() { |
31 |
| - m_inputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
32 |
| - const uint8_t desc1_val[] = {0x01}; |
33 |
| - m_inputReportDescriptor->setValue((uint8_t*)desc1_val, 1); |
34 |
| - m_inputReportNotifications = new BLE2902(); |
| 22 | + /* |
| 23 | + * Mandatory characteristic for device info service |
| 24 | + */ |
| 25 | + m_pnpCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a50, BLECharacteristic::PROPERTY_READ); |
35 | 26 |
|
36 |
| - m_outputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
37 |
| - const uint8_t desc2_val[] = {0x02}; |
38 |
| - m_outputReportDescriptor->setValue((uint8_t*)desc2_val, 1); |
| 27 | + /* |
| 28 | + * Mandatory characteristics for HID service |
| 29 | + */ |
| 30 | + m_hidInfoCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4a, BLECharacteristic::PROPERTY_READ); |
| 31 | + m_reportMapCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4b, BLECharacteristic::PROPERTY_READ); |
| 32 | + m_hidControlCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4c, BLECharacteristic::PROPERTY_WRITE_NR); |
| 33 | + m_protocolModeCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4e, BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_READ); |
39 | 34 |
|
40 |
| - m_featureReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
41 |
| - const uint8_t desc3_val[] = {0x03}; |
42 |
| - m_featureReportDescriptor->setValue((uint8_t*)desc3_val, 1); |
| 35 | + /* |
| 36 | + * Mandatory battery level characteristic with notification and presence descriptor |
| 37 | + */ |
| 38 | + BLE2904* batteryLevelDescriptor = new BLE2904(); |
| 39 | + batteryLevelDescriptor->setFormat(BLE2904::FORMAT_UINT8); |
| 40 | + batteryLevelDescriptor->setNamespace(1); |
| 41 | + batteryLevelDescriptor->setUnit(0x27ad); |
43 | 42 |
|
44 |
| - m_bootInputNotifications = new BLE2902(); |
| 43 | + m_batteryLevelCharacteristic = m_batteryService->createCharacteristic((uint16_t)0x2a19, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); |
| 44 | + m_batteryLevelCharacteristic->addDescriptor(batteryLevelDescriptor); |
| 45 | + m_batteryLevelCharacteristic->addDescriptor(new BLE2902()); |
45 | 46 |
|
46 |
| - if(m_batteryService != nullptr){ |
47 |
| - m_batteryLevelDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2904)); |
48 |
| - m_batteryLevelNotifications = new BLE2902(); |
49 |
| - } |
| 47 | + /* |
| 48 | + * This value is setup here because its default value in most usage cases, its very rare to use boot mode |
| 49 | + * and we want to simplify library using as much as possible |
| 50 | + */ |
| 51 | + const uint8_t pMode[] = {0x01}; |
| 52 | + protocolMode()->setValue((uint8_t*)pMode, 1); |
50 | 53 | }
|
51 | 54 |
|
52 |
| -void BLEHIDDevice::createCharacteristics() { |
53 |
| - m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a29, BLECharacteristic::PROPERTY_READ); |
54 |
| - m_pnpCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a50, BLECharacteristic::PROPERTY_READ); |
| 55 | +BLEHIDDevice::~BLEHIDDevice() { |
| 56 | +} |
55 | 57 |
|
56 |
| - m_hidInfoCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4a, BLECharacteristic::PROPERTY_READ); |
57 |
| - m_reportMapCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4b, BLECharacteristic::PROPERTY_READ); |
58 |
| - m_hidControlCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4c, BLECharacteristic::PROPERTY_WRITE_NR); |
59 |
| - m_inputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); |
60 |
| - m_outputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); |
61 |
| - m_featureReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR); |
62 |
| - m_protocolModeCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4e, BLECharacteristic::PROPERTY_WRITE_NR); |
63 |
| - m_bootInputCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a22, BLECharacteristic::PROPERTY_NOTIFY); |
64 |
| - m_bootOutputCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a32, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR); |
65 |
| - |
66 |
| - m_inputReportCharacteristic->addDescriptor(m_inputReportDescriptor); |
67 |
| - m_inputReportCharacteristic->addDescriptor(m_inputReportNotifications); |
68 |
| - m_outputReportCharacteristic->addDescriptor(m_outputReportDescriptor); |
69 |
| - m_featureReportCharacteristic->addDescriptor(m_featureReportDescriptor); |
70 |
| - m_bootInputCharacteristic->addDescriptor(m_bootInputNotifications); |
71 |
| - if(m_batteryService != nullptr){ |
72 |
| - m_batteryLevelCharacteristic->addDescriptor(m_batteryLevelDescriptor); //OPTIONAL? |
73 |
| - m_batteryLevelCharacteristic->addDescriptor(m_batteryLevelNotifications); //OPTIONAL? |
74 |
| - } |
| 58 | +/* |
| 59 | + * @brief |
| 60 | + */ |
| 61 | +void BLEHIDDevice::reportMap(uint8_t* map, uint16_t size) { |
| 62 | + m_reportMapCharacteristic->setValue(map, size); |
75 | 63 | }
|
76 | 64 |
|
| 65 | +/* |
| 66 | + * @brief This function suppose to be called at the end, when we have created all characteristics we need to build HID service |
| 67 | + */ |
77 | 68 | void BLEHIDDevice::startServices() {
|
78 | 69 | m_deviceInfoService->start();
|
79 | 70 | m_hidService->start();
|
80 |
| - if(m_batteryService!=nullptr) |
81 |
| - m_batteryService->start(); |
| 71 | + m_batteryService->start(); |
82 | 72 | }
|
83 | 73 |
|
84 |
| -BLEService* BLEHIDDevice::deviceInfo() { |
85 |
| - return m_deviceInfoService; |
| 74 | +/* |
| 75 | + * @brief Create manufacturer characteristic (this characteristic is optional) |
| 76 | + */ |
| 77 | +BLECharacteristic* BLEHIDDevice::manufacturer() { |
| 78 | + m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t)0x2a29, BLECharacteristic::PROPERTY_READ); |
| 79 | + return m_manufacturerCharacteristic; |
86 | 80 | }
|
87 | 81 |
|
88 |
| -BLEService* BLEHIDDevice::hidService() { |
89 |
| - return m_hidService; |
| 82 | +/* |
| 83 | + * @brief Set manufacturer name |
| 84
10000
code> | + * @param [in] name manufacturer name |
| 85 | + */ |
| 86 | +void BLEHIDDevice::manufacturer(std::string name) { |
| 87 | + m_manufacturerCharacteristic->setValue(name); |
90 | 88 | }
|
91 | 89 |
|
92 |
| -BLEService* BLEHIDDevice::batteryService() { |
93 |
| - return m_batteryService; |
| 90 | +/* |
| 91 | + * @brief |
| 92 | + */ |
| 93 | +void BLEHIDDevice::pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version) { |
| 94 | + 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}; |
| 95 | + m_pnpCharacteristic->setValue(pnp, sizeof(pnp)); |
94 | 96 | }
|
95 | 97 |
|
96 |
| -BLECharacteristic* BLEHIDDevice::manufacturer() { |
97 |
| - return m_manufacturerCharacteristic; |
| 98 | +/* |
| 99 | + * @brief |
| 100 | + */ |
| 101 | +void BLEHIDDevice::hidInfo(uint8_t country, uint8_t flags) { |
| 102 | + uint8_t info[] = {0x11,0x1, country, flags}; |
| 103 | + m_hidInfoCharacteristic->setValue(info, sizeof(info));; |
98 | 104 | }
|
99 | 105 |
|
100 |
| -BLECharacteristic* BLEHIDDevice::pnp() { |
101 |
| - return m_pnpCharacteristic; |
102 |
| -} |
| 106 | +/* |
| 107 | + * @brief Create input report characteristic that need to be saved as new characteristic object so can be further used |
| 108 | + * @param [in] reportID input report ID, the same as in report map for input object related to created characteristic |
| 109 | + * @return pointer to new input report characteristic |
| 110 | + */ |
| 111 | +BLECharacteristic* BLEHIDDevice::inputReport(uint8_t reportID) { |
| 112 | + BLECharacteristic* inputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); |
| 113 | + BLEDescriptor* inputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
103 | 114 |
|
104 |
| -BLECharacteristic* BLEHIDDevice::hidInfo() { |
105 |
| - return m_hidInfoCharacteristic; |
| 115 | + uint8_t desc1_val[] = {reportID, 0x01}; |
| 116 | + inputReportDescriptor->setValue((uint8_t*)desc1_val, 2); |
| 117 | + inputReportCharacteristic->addDescriptor(new BLE2902()); |
| 118 | + inputReportCharacteristic->addDescriptor(inputReportDescriptor); |
| 119 | + |
| 120 | + return inputReportCharacteristic; |
106 | 121 | }
|
107 | 122 |
|
108 |
| -BLECharacteristic* BLEHIDDevice::reportMap() { |
109 |
| - return m_reportMapCharacteristic; |
| 123 | +/* |
| 124 | + * @brief Create output report characteristic that need to be saved as new characteristic object so can be further used |
| 125 | + * @param [in] reportID Output report ID, the same as in report map for output object related to created characteristic |
| 126 | + * @return Pointer to new output report characteristic |
| 127 | + */ |
| 128 | +BLECharacteristic* BLEHIDDevice::outputReport(uint8_t reportID) { |
| 129 | + BLECharacteristic* outputReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR); |
| 130 | + BLEDescriptor* outputReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
| 131 | + |
| 132 | + uint8_t desc1_val[] = {reportID, 0x02}; |
| 133 | + outputReportDescriptor->setValue((uint8_t*)desc1_val, 2); |
| 134 | + outputReportCharacteristic->addDescriptor(outputReportDescriptor); |
| 135 | + |
| 136 | + return outputReportCharacteristic; |
110 | 137 | }
|
111 | 138 |
|
112 |
| -BLECharacteristic* BLEHIDDevice::hidControl() { |
113 |
| - return m_hidControlCharacteristic; |
| 139 | +/* |
| 140 | + * @brief Create feature report characteristic that need to be saved as new characteristic object so can be further used |
| 141 | + * @param [in] reportID Feature report ID, the same as in report map for feature object related to created characteristic |
| 142 | + * @return Pointer to new feature report characteristic |
| 143 | + */ |
| 144 | +BLECharacteristic* BLEHIDDevice::featureReport(uint8_t reportID) { |
| 145 | + BLECharacteristic* featureReportCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a4d, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); |
| 146 | + BLEDescriptor* featureReportDescriptor = new BLEDescriptor(BLEUUID((uint16_t)0x2908)); |
| 147 | + |
| 148 | + uint8_t desc1_val[] = {reportID, 0x03}; |
| 149 | + featureReportDescriptor->setValue((uint8_t*)desc1_val, 2); |
| 150 | + featureReportCharacteristic->addDescriptor(featureReportDescriptor); |
| 151 | + |
| 152 | + return featureReportCharacteristic; |
114 | 153 | }
|
115 | 154 |
|
116 |
| -BLECharacteristic* BLEHIDDevice::inputReport(void*) { |
117 |
| - return m_inputReportCharacteristic; |
| 155 | +/* |
| 156 | + * @brief |
| 157 | + */ |
| 158 | +BLECharacteristic* BLEHIDDevice::bootInput() { |
| 159 | + BLECharacteristic* bootInputCharacteristic = m_hidService->createCharacteristic((uint16_t)0x2a22, BLECharacteristic::PROPERTY_NOTIFY); |
| 160 | + bootInputCharacteristic->addDescriptor(new BLE2902()); |
| 161 | + |
| 162 | + return bootInputCharacteristic; |
118 | 163 | }
|
119 | 164 |
|
120 |
| -BLECharacteristic* BLEHIDDevice::outputReport(void*) { |
121 |
| - return m_outputReportCharacteristic; |
| 165 | +/* |
| 166 | + * @brief |
| 167 | + */ |
| 168 | +BLECharacteristic* BLEHIDDevice::bootOutput() { |
| 169 | + return m_hidService->createCharacteristic((uint16_t)0x2a32, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR); |
122 | 170 | }
|
123 | 171 |
|
124 |
| -BLECharacteristic* BLEHIDDevice::featureReport(void*) { |
125 |
| - return m_featureReportCharacteristic; |
| 172 | +/* |
| 173 | + * @brief |
| 174 | + */ |
| 175 | +BLECharacteristic* BLEHIDDevice::hidControl() { |
| 176 | + return m_hidControlCharacteristic; |
126 | 177 | }
|
127 | 178 |
|
| 179 | +/* |
| 180 | + * @brief |
| 181 | + */ |
128 | 182 | BLECharacteristic* BLEHIDDevice::protocolMode() {
|
129 | 183 | return m_protocolModeCharacteristic;
|
130 | 184 | }
|
131 | 185 |
|
132 |
| -BLECharacteristic* BLEHIDDevice::bootInput() { |
133 |
| - return m_bootInputCharacteristic; |
| 186 | +void BLEHIDDevice::setBatteryLevel(uint8_t level) { |
| 187 | + m_batteryLevelCharacteristic->setValue(&level, 1); |
134 | 188 | }
|
135 |
| - |
136 |
| -BLECharacteristic* BLEHIDDevice::bootOutput() { |
137 |
| - return m_bootOutputCharacteristic; |
| 189 | +/* |
| 190 | + * @brief Returns battery level characteristic |
| 191 | + * @ return battery level characteristic |
| 192 | + *//* |
| 193 | +BLECharacteristic* BLEHIDDevice::batteryLevel() { |
| 194 | + return m_batteryLevelCharacteristic; |
138 | 195 | }
|
139 | 196 |
|
140 |
| -BLECharacteristic* BLEHIDDevice::batteryLevel(void*) { |
141 |
| - return m_batteryLevelCharacteristic; |
| 197 | +
|
| 198 | +
|
| 199 | +BLECharacteristic* BLEHIDDevice::reportMap() { |
| 200 | + return m_reportMapCharacteristic; |
142 | 201 | }
|
143 | 202 |
|
144 |
| -BLEDescriptor* BLEHIDDevice::inputReport() { |
145 |
| - return m_inputReportDescriptor; |
| 203 | +BLECharacteristic* BLEHIDDevice::pnp() { |
| 204 | + return m_pnpCharacteristic; |
146 | 205 | }
|
147 | 206 |
|
148 |
| -BLEDescriptor* BLEHIDDevice::outputReport() { |
149 |
| - return m_outputReportDescriptor; |
| 207 | +
|
| 208 | +BLECharacteristic* BLEHIDDevice::hidInfo() { |
| 209 | + return m_hidInfoCharacteristic; |
| 210 | +} |
| 211 | +*/ |
| 212 | +/* |
| 213 | + * @brief |
| 214 | + */ |
| 215 | +BLEService* BLEHIDDevice::deviceInfo() { |
| 216 | + return m_deviceInfoService; |
150 | 217 | }
|
151 | 218 |
|
152 |
| -BLEDescriptor* BLEHIDDevice::featureReport() { |
153 |
| - return m_featureReportDescriptor; |
| 219 | +/* |
| 220 | + * @brief |
| 221 | + */ |
| 222 | +BLEService* BLEHIDDevice::hidService() { |
| 223 | + return m_hidService; |
154 | 224 | }
|
155 | 225 |
|
156 |
| -BLEDescriptor* BLEHIDDevice::batteryLevel() { |
157 |
| - return m_batteryLevelDescriptor; |
| 226 | +/* |
| 227 | + * @brief |
| 228 | + */ |
| 229 | +BLEService* BLEHIDDevice::batteryService() { |
| 230 | + return m_batteryService; |
158 | 231 | }
|
159 | 232 |
|
160 | 233 | #endif // CONFIG_BT_ENABLED
|
| 234 | + |
0 commit comments