Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the same report ID in multiple input/output/feature reports #833

Closed
wants to merge 9 commits into from
30 changes: 7 additions & 23 deletions src/NimBLEHIDDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,20 @@ void NimBLEHIDDevice::setBatteryLevel(uint8_t level, bool notify) {
} // setBatteryLevel

/**
* @brief Locate the characteristic for a report ID.
* @brief Locate the characteristic for a report ID and a report type.
*
* @param [in] reportId Report identifier to locate.
* @param [out] reportType Type of report (input/output/feature). Not meaningful if the return value is nullptr.
* @param [in] reportType Type of report (input/output/feature).
* @return NimBLECharacteristic* The characteristic.
* @return nullptr If the characteristic does not exist.
*/
NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicById(uint8_t reportId, uint8_t& reportType) {
NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicByIdAndType(uint8_t reportId, uint8_t reportType) {
NimBLECharacteristic* candidate = m_hidSvc->getCharacteristic(inputReportChrUuid, 0);
for (uint16_t i = 1; (candidate != nullptr) && (i != 0); i++) {
NimBLEDescriptor* dsc = candidate->getDescriptorByUUID(featureReportDscUuid);
NimBLEAttValue desc1_val_att = dsc->getValue();
const uint8_t* desc1_val = desc1_val_att.data();
reportType = desc1_val[1];
if (desc1_val[0] == reportId) return candidate;
if ((desc1_val[0] == reportId) && (desc1_val[1] == reportType)) return candidate;
candidate = m_hidSvc->getCharacteristic(inputReportChrUuid, i);
}
return nullptr;
Expand All @@ -175,15 +174,10 @@ NimBLECharacteristic* NimBLEHIDDevice::locateReportCharacteristicById(uint8_t re
* @param [in] reportId Input report ID, the same as in report map for input object related to the characteristic.
* @return NimBLECharacteristic* A pointer to the input report characteristic.
* Store this value to avoid computational overhead.
* @return nullptr If the report is already created as an output or feature report.
* @details This will create the characteristic if not already created.
*/
NimBLECharacteristic* NimBLEHIDDevice::getInputReport(uint8_t reportId) {
uint8_t reportType;
NimBLECharacteristic* inputReportChr = locateReportCharacteristicById(reportId, reportType);
if ((inputReportChr != nullptr) && (reportType != 0x01))
// ERROR: this reportId exists, but it is not an input report
return nullptr;
NimBLECharacteristic* inputReportChr = locateReportCharacteristicByIdAndType(reportId, 0x01);
if (inputReportChr == nullptr) {
inputReportChr =
m_hidSvc->createCharacteristic(inputReportChrUuid,
Expand All @@ -203,15 +197,10 @@ NimBLECharacteristic* NimBLEHIDDevice::getInputReport(uint8_t reportId) {
* @param [in] reportId Output report ID, the same as in report map for output object related to the characteristic.
* @return NimBLECharacteristic* A pointer to the output report characteristic.
* Store this value to avoid computational overhead.
* @return nullptr If the report is already created as an input or feature report.
* @details This will create the characteristic if not already created.
*/
NimBLECharacteristic* NimBLEHIDDevice::getOutputReport(uint8_t reportId) {
uint8_t reportType;
NimBLECharacteristic* outputReportChr = locateReportCharacteristicById(reportId, reportType);
if ((outputReportChr != nullptr) && (reportType != 0x02))
// ERROR: this reportId exists, but it is not an output report
return nullptr;
NimBLECharacteristic* outputReportChr = locateReportCharacteristicByIdAndType(reportId, 0x02);
if (outputReportChr == nullptr) {
outputReportChr =
m_hidSvc->createCharacteristic(inputReportChrUuid,
Expand All @@ -232,15 +221,10 @@ NimBLECharacteristic* NimBLEHIDDevice::getOutputReport(uint8_t reportId) {
* @param [in] reportId Feature report ID, the same as in report map for feature object related to the characteristic.
* @return NimBLECharacteristic* A pointer to feature report characteristic.
* Store this value to avoid computational overhead.
* @return nullptr If the report is already created as an input or output report.
* @details This will create the characteristic if not already created.
*/
NimBLECharacteristic* NimBLEHIDDevice::getFeatureReport(uint8_t reportId) {
uint8_t reportType;
NimBLECharacteristic* featureReportChr = locateReportCharacteristicById(reportId, reportType);
if ((featureReportChr != nullptr) && (reportType != 0x03))
// ERROR: this reportId exists, but it is not a feature report
return nullptr;
NimBLECharacteristic* featureReportChr = locateReportCharacteristicByIdAndType(reportId, 0x03);
if (featureReportChr == nullptr) {
featureReportChr = m_hidSvc->createCharacteristic(
inputReportChrUuid,
Expand Down
2 changes: 1 addition & 1 deletion src/NimBLEHIDDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class NimBLEHIDDevice {
NimBLECharacteristic* m_protocolModeChr{nullptr}; // 0x2a4e
NimBLECharacteristic* m_batteryLevelChr{nullptr}; // 0x2a19

NimBLECharacteristic* locateReportCharacteristicById(uint8_t reportId, uint8_t& reportType);
NimBLECharacteristic* locateReportCharacteristicByIdAndType(uint8_t reportId, uint8_t reportType);
};

#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
Expand Down
Loading