diff --git a/components/hid_service/example/main/hid_service_example.cpp b/components/hid_service/example/main/hid_service_example.cpp index 3e8c4d5d7..d0c6b1e86 100644 --- a/components/hid_service/example/main/hid_service_example.cpp +++ b/components/hid_service/example/main/hid_service_example.cpp @@ -106,7 +106,7 @@ extern "C" void app_main(void) { hid_service.set_report_map(descriptor); // use the HID service to make an input report characteristic - auto input_report = hid_service.input_report(input_report_id); + [[maybe_unused]] auto input_report = hid_service.input_report(input_report_id); // use the HID service to make an output report characteristic [[maybe_unused]] auto output_report = hid_service.output_report(output_report_id); @@ -217,7 +217,11 @@ extern "C" void app_main(void) { // send an input report auto report = gamepad_input_report.get_report(); logger.debug("Sending report data ({}): {::#02x}", report.size(), report); - input_report->notify(report); + + // Get the stored pointer (we could use the one above, but this is just to + // show how to get it) + auto report_char = hid_service.input_report(input_report_id); + report_char->notify(report); // sleep std::this_thread::sleep_until(start + 1s); diff --git a/components/hid_service/include/hid_service.hpp b/components/hid_service/include/hid_service.hpp index 6a22e5b43..075c4832b 100644 --- a/components/hid_service/include/hid_service.hpp +++ b/components/hid_service/include/hid_service.hpp @@ -177,6 +177,26 @@ class HidService : public espp::BaseComponent { return input_report_char; } + /// @brief Remove an input report characteristic. + /// @param report_id The report ID of the input report characteristic to + /// remove. + /// @note This will delete the characteristic, and remove it from the list. + /// Any stored pointers to the characteristic will be invalid after this + /// call. + void remove_input_report(uint8_t report_id) { + std::lock_guard lock(input_report_characteristics_mutex_); + auto it = + std::find_if(input_report_characteristics_.begin(), input_report_characteristics_.end(), + [report_id](const ReportCharacteristic &report_char) { + return report_char.first == report_id; + }); + if (it != input_report_characteristics_.end()) { + static constexpr bool delete_char = true; + service_->removeCharacteristic(it->second, delete_char); + input_report_characteristics_.erase(it); + } + } + /// @brief Create an output report characteristic. /// @param report_id The report ID. This should be the same as the report /// ID in the report descriptor for the output object that @@ -213,6 +233,26 @@ class HidService : public espp::BaseComponent { return output_report_char; } + /// @brief Remove an output report characteristic. + /// @param report_id The report ID of the output report characteristic to + /// remove. + /// @note This will delete the characteristic, and remove it from the list. + /// Any stored pointers to the characteristic will be invalid after this + /// call. + void remove_output_report(uint8_t report_id) { + std::lock_guard lock(output_report_characteristics_mutex_); + auto it = + std::find_if(output_report_characteristics_.begin(), output_report_characteristics_.end(), + [report_id](const ReportCharacteristic &report_char) { + return report_char.first == report_id; + }); + if (it != output_report_characteristics_.end()) { + static constexpr bool delete_char = true; + service_->removeCharacteristic(it->second, delete_char); + output_report_characteristics_.erase(it); + } + } + /// @brief Create a feature report characteristic. /// @param report_id The report ID. This should be the same as the report /// ID in the report descriptor for the feature object that @@ -248,6 +288,26 @@ class HidService : public espp::BaseComponent { return feature_report_char; } + /// @brief Remove a feature report characteristic. + /// @param report_id The report ID of the feature report characteristic to + /// remove. + /// @note This will delete the characteristic, and remove it from the list. + /// Any stored pointers to the characteristic will be invalid after this + /// call. + void remove_feature_report(uint8_t report_id) { + std::lock_guard lock(feature_report_characteristics_mutex_); + auto it = + std::find_if(feature_report_characteristics_.begin(), feature_report_characteristics_.end(), + [report_id](const ReportCharacteristic &report_char) { + return report_char.first == report_id; + }); + if (it != feature_report_characteristics_.end()) { + static constexpr bool delete_char = true; + service_->removeCharacteristic(it->second, delete_char); + feature_report_characteristics_.erase(it); + } + } + protected: static constexpr uint16_t SERVICE_UUID = 0x1812; static constexpr uint16_t HID_INFORMATION_UUID = 0x2a4a;