Skip to content

Commit aae70df

Browse files
afpinedah2zero
authored andcommitted
Allow the same report ID in multiple input/output/feature reports
1 parent ad12a48 commit aae70df

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

src/NimBLEHIDDevice.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,20 @@ void NimBLEHIDDevice::setBatteryLevel(uint8_t level, bool notify) {
150150
} // setBatteryLevel
151151

152152
/**
153-
* @brief Locate the characteristic for a report ID.
153+
* @brief Locate the characteristic for a report ID and a report type.
154154
*
155155
* @param [in] reportId Report identifier to locate.
156-
* @param [out] reportType Type of report (input/output/feature). Not meaningful if the return value is nullptr.
156+
* @param [in] reportType Type of report (input/output/feature).
157157
* @return NimBLECharacteristic* The characteristic.
158158
* @return nullptr If the characteristic does not exist.
159159
*/
160-
NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicById(uint8_t reportId, uint8_t& reportType) {
160+
NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicByIdAndType(uint8_t reportId, uint8_t reportType) {
161161
NimBLECharacteristic* candidate = m_hidSvc->getCharacteristic(inputReportChrUuid, 0);
162162
for (uint16_t i = 1; (candidate != nullptr) && (i != 0); i++) {
163163
NimBLEDescriptor* dsc = candidate->getDescriptorByUUID(featureReportDscUuid);
164164
NimBLEAttValue desc1_val_att = dsc->getValue();
165165
const uint8_t* desc1_val = desc1_val_att.data();
166-
reportType = desc1_val[1];
167-
if (desc1_val[0] == reportId) return candidate;
166+
if ((desc1_val[0] == reportId) && (desc1_val[1] == reportType)) return candidate;
168167
candidate = m_hidSvc->getCharacteristic(inputReportChrUuid, i);
169168
}
170169
return nullptr;
@@ -175,15 +174,10 @@ NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicById(uint8_t re
175174
* @param [in] reportId Input report ID, the same as in report map for input object related to the characteristic.
176175
* @return NimBLECharacteristic* A pointer to the input report characteristic.
177176
* Store this value to avoid computational overhead.
178-
* @return nullptr If the report is already created as an output or feature report.
179177
* @details This will create the characteristic if not already created.
180178
*/
181179
NimBLECharacteristic* NimBLEHIDDevice::getInputReport(uint8_t reportId) {
182-
uint8_t reportType;
183-
NimBLECharacteristic* inputReportChr = locateReportCharacteristicById(reportId, reportType);
184-
if ((inputReportChr != nullptr) && (reportType != 0x01))
185-
// ERROR: this reportId exists, but it is not an input report
186-
return nullptr;
180+
NimBLECharacteristic* inputReportChr = locateReportCharacteristicByIdAndType(reportId, 0x01);
187181
if (inputReportChr == nullptr) {
188182
inputReportChr =
189183
m_hidSvc->createCharacteristic(inputReportChrUuid,
@@ -203,15 +197,10 @@ NimBLECharacteristic* NimBLEHIDDevice::getInputReport(uint8_t reportId) {
203197
* @param [in] reportId Output report ID, the same as in report map for output object related to the characteristic.
204198
* @return NimBLECharacteristic* A pointer to the output report characteristic.
205199
* Store this value to avoid computational overhead.
206-
* @return nullptr If the report is already created as an input or feature report.
207200
* @details This will create the characteristic if not already created.
208201
*/
209202
NimBLECharacteristic* NimBLEHIDDevice::getOutputReport(uint8_t reportId) {
210-
uint8_t reportType;
211-
NimBLECharacteristic* outputReportChr = locateReportCharacteristicById(reportId, reportType);
212-
if ((outputReportChr != nullptr) && (reportType != 0x02))
213-
// ERROR: this reportId exists, but it is not an output report
214-
return nullptr;
203+
NimBLECharacteristic* outputReportChr = locateReportCharacteristicByIdAndType(reportId, 0x02);
215204
if (outputReportChr == nullptr) {
216205
outputReportChr =
217206
m_hidSvc->createCharacteristic(inputReportChrUuid,
@@ -232,15 +221,10 @@ NimBLECharacteristic* NimBLEHIDDevice::getOutputReport(uint8_t reportId) {
232221
* @param [in] reportId Feature report ID, the same as in report map for feature object related to the characteristic.
233222
* @return NimBLECharacteristic* A pointer to feature report characteristic.
234223
* Store this value to avoid computational overhead.
235-
* @return nullptr If the report is already created as an input or output report.
236224
* @details This will create the characteristic if not already created.
237225
*/
238226
NimBLECharacteristic* NimBLEHIDDevice::getFeatureReport(uint8_t reportId) {
239-
uint8_t reportType;
240-
NimBLECharacteristic* featureReportChr = locateReportCharacteristicById(reportId, reportType);
241-
if ((featureReportChr != nullptr) && (reportType != 0x03))
242-
// ERROR: this reportId exists, but it is not a feature report
243-
return nullptr;
227+
NimBLECharacteristic* featureReportChr = locateReportCharacteristicByIdAndType(reportId, 0x03);
244228
if (featureReportChr == nullptr) {
245229
featureReportChr = m_hidSvc->createCharacteristic(
246230
inputReportChrUuid,

src/NimBLEHIDDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class NimBLEHIDDevice {
8282
NimBLECharacteristic* m_protocolModeChr{nullptr}; // 0x2a4e
8383
NimBLECharacteristic* m_batteryLevelChr{nullptr}; // 0x2a19
8484

85-
NimBLECharacteristic* locateReportCharacteristicById(uint8_t reportId, uint8_t& reportType);
85+
NimBLECharacteristic* locateReportCharacteristicByIdAndType(uint8_t reportId, uint8_t reportType);
8686
};
8787

8888
#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)

0 commit comments

Comments
 (0)